package controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import pojo.Book;

@Controller
public class BookController {
	private List<Book> data = new ArrayList<>();
	
	public BookController() {
		System.out.println("co....");
		for (int i = 0; i < 10; i++) {
			Book b = new Book(i, "book" + i, i * 12.0);
			data.add(b);
		}
	}
	@RequestMapping("/addBook")
	public String addBook(Book b){
		data.add(b);
		return "redirect:/findAllBooks";
	}
	@RequestMapping("/findAllBooks")
	public String findAllBooks(Model model){
		model.addAttribute("bks", data);
		
		return "bookList";
	}
	@RequestMapping("/deleteBook")
	public String deleteBook(int id){
		int x = -1;
		for(int i = 0; i < data.size(); i++) {
			Book b = data.get(i);
			if (id == b.getId()) {
				x=i;
				break;
				
			}
		}
		data.remove(x);
		return "forward:/findAllBooks";
	}
	
	@RequestMapping("/book/{id}/author/{aid}")
	public String bookDetail(@PathVariable(value="id") int ids, @PathVariable(value="aid") int aid){
		System.out.println("id:" + ids + "...aid:" + aid);
		return "suc";
	}
	@RequestMapping("/upload")
	public String uploadFile(MultipartFile headImg) {
		String ofileName = headImg.getOriginalFilename();
		long size = headImg.getSize();
		System.out.println(ofileName + "......" + size);
		
		return "suc";
		
	}
}
